<?php

class Bs_player {
	public function __construct() {
	   add_action( 'admin_enqueue_scripts', array(&$this, 'bs_admin_enqueue_scripts') );
	   add_action( 'init', array(&$this, 'create_player_post_type') );
	   add_filter( 'manage_players_posts_columns', array(&$this, 'replace_player_columns'), 100 );
	   add_action( 'manage_players_posts_custom_column', array(&$this, 'add_player_columns_data') );
	   add_action( 'init', array(&$this,'players_taxanomy_init' ) );
	   add_action( 'admin_head', array(&$this, 'remove_seo_filter'), 20 );
	   add_filter( 'manage_edit-players_sortable_columns', array(&$this, 'sortable_columns') );
	   add_action( 'pre_get_posts', array(&$this, 'columns_orderby') );
	}
	
	/**
	 * Add stylesheets and scripts only for admin
	 */
	public function bs_admin_enqueue_scripts($hook)
	{
	   wp_register_style( 'bs-admin-css', BS_ASSETS_URL . 'css/admin.css', false, '1.0.0' );
	   wp_enqueue_style( 'bs-admin-css' );
	   
	   wp_enqueue_script( 'bs-admin-js', BS_ASSETS_URL. 'js/admin.js', array('jquery'), '1.0.0' );
	}

	public function create_player_post_type() {
		register_post_type('players',
			array(
				'labels' => array(
					'name' => __( 'Spillere' ),
					'singular_name' => __( 'Spiller' )
				),
				'public' => true,
      			'has_archive' => true,
      			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
			)
		);
	}

	public function replace_player_columns($columns) {

		$new_columns = array(
			'boot' => __('Støvle', 'fodfessor'),
			'club' => __('Klub', 'fodfessor'),
			'position' => __('Position', 'fodfessor'),
			'nation' => __('Nation', 'fodfessor'),
			'foot' => __('Fod', 'fodfessor'),
			'birthday' => __('Fødselsdag', 'fodfessor'),
			'club_history' => __('Klubhistorik', 'fodfessor')
		);
		return array_merge(array_slice($columns, 0, 3), $new_columns);
	}

	public function add_player_columns_data($column) {

		switch ($column) {

			case 'thumbnail':
				$value = get_post_meta(get_the_ID());
				echo $value[$column[0]];
				break;

			case 'boot':
				$id = get_field($column, get_the_ID());
				echo get_the_title($id);
				break;

			case 'club':
			   	$id = get_field($column, get_the_ID());
				echo get_the_title($id);
				break;
			
			case 'position':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;

			case 'nation':
				$value = get_field($column, get_the_ID());
				echo $value;
				break;

			case 'foot':
				$value = get_field($column, get_the_ID());
				echo $value;
				break;

			case 'birthday':
				$value = get_field($column, get_the_ID());
				echo $value;
				break;

			case 'club_history':
				$value = get_field($column, get_the_ID());
				echo $value;
				break;
		}
	}

	public function sortable_columns( $columns ) {
		$columns['boot'] = 'boot';
	    $columns['club'] = 'club';
	    $columns['position'] = 'position';
	    $columns['nation'] = 'nation';
	    $columns['foot'] = 'foot';
	    $columns['birthday'] = 'birthday';
	 
	    return $columns;
	}

	public function columns_orderby( $query ) {
	    if( ! is_admin() )
	        return;
	 
	 	$valid_columns = array('boot', 'club', 'nation', 'foot', 'birthday');
	    $orderby = $query->get( 'orderby');
	 
	 	if(in_array($orderby, $valid_columns)) {
	 		$query->set('meta_key', $orderby);
	 		$query->set('orderby','meta_value');
	 	}
	}

	public function remove_seo_filter() {
	    global $wpseo_metabox;
	    global $post;
	    $post_id = get_post_type($post->id);

	    if($post_id === 'players') {

		    if ( $wpseo_metabox ) {
		        remove_action( 'restrict_manage_posts', array( $wpseo_metabox, 'posts_filter_dropdown' ) );
		    }
		}
	}

	public function players_taxanomy_init() {
	// create a new taxonomy
		register_taxonomy(
			'position',
			'players',
			array(
				'label' => __( "Positioner" ),
				'rewrite' => array( 'slug' => 'position' ),
				'hierarchical' => false
			)
		);
	}
}

new Bs_player(); 